package com.skp.shaphan.ui.buttons;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import com.skp.shaphan.SQLEditorsPane;
import com.skp.shaphan.ShaphanApp;
import com.skp.shaphan.ui.dialogs.NavigationSearchDialog;

public class NavSearchOkButton extends JButton implements ActionListener, AncestorListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5881384355538806782L;
	private NavigationSearchDialog dialog;
	
	public NavSearchOkButton(NavigationSearchDialog dialog) {
		super("Search");
		this.dialog = dialog;
		this.addActionListener(this);
		this.addAncestorListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		SQLEditorsPane editors = ShaphanApp.getMainWindow().getSQLEditorsPane();
		Connection conn = editors.getConnection().getConnection();
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		ArrayList<String> portalNames = new ArrayList<String>();
		ArrayList<String> portalLabels = new ArrayList<String>();
		ArrayList<String> portalObjNames = new ArrayList<String>();
		String path;
		String parentObjName;
		try {
//			SELECT TOP 100 * FROM PSPRSMDEFN
//			WHERE PORTAL_NAME = 'EMPLOYEE';
//
//			PORTAL_OBJNAME = 'PT_PORTAL_OBJ_LIST'
//			PORTAL_PRNTOBJNAME = 'PT_PORTAL'
//			PORTAL_LABEL = 'Structure and Content'
//			PORTAL_URI_SEG1 = 'PORTAL_ADMIN'
//			PORTAL_URI_SEG2 = 'PORTAL_OBJ_LIST'
			PreparedStatement st = conn.prepareStatement("select * from PSPRSMDEFN where PORTAL_URI_SEG2 = ?");
			st.setString(1, dialog.getComponentName());
			ResultSet rs = st.executeQuery();
			while(rs.next()) {
				portalNames.add(rs.getString("PORTAL_NAME"));
				portalLabels.add(rs.getString("PORTAL_LABEL"));
				portalObjNames.add(rs.getString("PORTAL_PRNTOBJNAME"));
			}
			rs.close();
			st.close();
			
			st = conn.prepareStatement("select * from PSPRSMDEFN where PORTAL_OBJNAME = ?");
			for(int x=0; x<portalNames.size(); x++) {
				path = portalLabels.get(x);
				st.setString(1, portalObjNames.get(x));
				rs = st.executeQuery();
				if(rs.next()) {
					parentObjName = rs.getString("PORTAL_PRNTOBJNAME");
					path = rs.getString("PORTAL_LABEL") + " > " + path;
				} else {
					parentObjName = "";
				}
				rs.close();
				while(parentObjName.trim().length() > 0) {
					st.setString(1, parentObjName);
					rs = st.executeQuery();
					if(rs.next()) {
						parentObjName = rs.getString("PORTAL_PRNTOBJNAME");
						path = rs.getString("PORTAL_LABEL") + " > " + path;
					} else {
						parentObjName = "";
					}
					rs.close();
				}
				pw.println("Portal = " + portalNames.get(x) + " - " + path);
			}
			st.close();
			
			dialog.setSearchResults(sw.toString());
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		//dialog.dispose();
	}

	@Override
	public void ancestorAdded(AncestorEvent event) {
		this.getRootPane().setDefaultButton(this);
	}

	@Override
	public void ancestorMoved(AncestorEvent event) {
	}

	@Override
	public void ancestorRemoved(AncestorEvent event) {
	}
}
